package com.jeecms.wicool.action.car;

import static com.jeecms.common.page.SimplePage.cpn;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.car.CarOrderConf;
import com.jeecms.wicool.entity.car.CarOrderConfDetail;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.entity.util.Dictionary2;
import com.jeecms.wicool.manager.car.CarOrderConfDetailMng;
import com.jeecms.wicool.manager.car.CarOrderConfMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

@Controller
public class CarOrderConfAct {
	private static final Logger log = LoggerFactory.getLogger(CarOrderConfAct.class);

	@RequestMapping("/carOrderConf/v_list.do")
	public String list(Short orderType,String name ,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage( orderType, name, cpn(pageNo),
				CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		List<CarOrderConf> listCarOrderConf=(List<CarOrderConf>)pagination.getList();
		List<Dictionary2> listOrderType=DicMng.getList("order_type");
		List<Dictionary2> listHaveAdviser=DicMng.getList("yes_no");
		Map<String,String> map = new HashMap<String,String>();
		Map<String,String> map2 = new HashMap<String,String>();
		 for(Dictionary2 d : listOrderType){
			 map.put(d.getDicCode().toString(), d.getDicName());
		 } for(Dictionary2 d : listHaveAdviser){
			 map2.put(d.getDicCode().toString(), d.getDicName());
		 }
		 for(CarOrderConf p : listCarOrderConf){
			 if(map.containsKey(p.getOrderType().toString())){
				 p.setOrderTypeName(map.get(p.getOrderType().toString()));
			 }
			 if(map2.containsKey(p.getHaveAdviser().toString())){
				 p.setHaveAdviserName(map2.get(p.getHaveAdviser().toString()));
			 }
		 }
		 model.addAttribute("pagination", pagination);
		model.addAttribute("listOrderType",listOrderType);
		model.addAttribute("orderType",orderType);
		model.addAttribute("name",name);
		return "car/list_carOrderConf";
	}
	@RequestMapping("/carOrderConf/v_toEditCarOrderConf.do")
	public String toEditCarOrderConf(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		model.addAttribute("carOrderConf_id",id);
		return "car/to_edit_carOrderConf";
	}
	@RequestMapping("/carOrderConf/v_edit.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarOrderConf carOrderConf = manager.findById(id);
		
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<Dictionary2> listOrderType=DicMng.getList("order_type");
		model.addAttribute("listOrderType",listOrderType);
		model.addAttribute("carOrderConf",carOrderConf);
		model.addAttribute("listShop",listShop);
		return "car/edit_carOrderConf";
	}
	@RequestMapping("/carOrderConf/v_edit_carOrderConfDetail.do")
	public String editCarOrderConfDetail(Integer carOrderConfId,HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(carOrderConfId, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarOrderConf carOrderConf = manager.findById(carOrderConfId);
		CarOrderConfDetail carOrderConfDetail = carOrderConfDetailMng.getByCarOrderConfId(carOrderConfId);
		if(carOrderConfDetail==null){
			carOrderConfDetail=new CarOrderConfDetail();
		}
		carOrderConfDetail.setCarOrderConf(carOrderConf);
		List<Dictionary2> listDataType=DicMng.getList("data_type");
		model.addAttribute("listDataType",listDataType);
		model.addAttribute("carOrderConfDetail",carOrderConfDetail);
		return "car/edit_carOrderConfDetail";
	}
	@RequestMapping("/carOrderConf/v_toView.do")
	public String toView(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		model.addAttribute("carOrderConf_id",id);
		return "car/to_view_carOrderConf";
	}
	@RequestMapping("/carOrderConf/v_view.do")
	public String view(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarOrderConf carOrderConf = manager.findById(id);
		List<Dictionary2> listOrderType=DicMng.getList("order_type");
		List<Dictionary2> listHaveAdviser=DicMng.getList("yes_no");
		if(listOrderType!=null){
			for(Dictionary2 d :listOrderType){
				if(carOrderConf.getOrderType().equals(d.getDicCode())){
					carOrderConf.setOrderTypeName(d.getDicName());
				}
			}
		}if(listHaveAdviser!=null){
			for(Dictionary2 d :listHaveAdviser){
				if(carOrderConf.getHaveAdviser().equals(d.getDicCode())){
					carOrderConf.setHaveAdviserName(d.getDicName());
				}
			}
		}
		
		model.addAttribute("carOrderConf",carOrderConf);
		return "car/view_carOrderConf";
	}
	@RequestMapping("/carOrderConf/v_view_carOrderConfDetail.do")
	public String viewCarOrderConfDetail(Integer carOrderConfId, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(carOrderConfId, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarOrderConfDetail carOrderConfDetail = carOrderConfDetailMng.getByCarOrderConfId(carOrderConfId);
		if(carOrderConfDetail!=null){
		List<Dictionary2> listDataType=DicMng.getList("data_type");
		List<Dictionary2> listStatus=DicMng.getList("status_valid");
		if(listDataType!=null){
			for(Dictionary2 d :listDataType){
				if(carOrderConfDetail.getDataType().equals(d.getDicCode())){
					carOrderConfDetail.setDataTypeName(d.getDicName());
				}
			}
		}if(listStatus!=null){
			for(Dictionary2 d :listStatus){
				if(carOrderConfDetail.getStatus().equals(d.getDicCode())){
					carOrderConfDetail.setStatusName(d.getDicName());
				}
			}
		}
		model.addAttribute("carOrderConfDetail",carOrderConfDetail);
		}else{
			model.addAttribute("carOrderConfDetail",new CarOrderConfDetail());
		}
		
		return "car/view_carOrderConfDetail";
	}
	@RequestMapping("/carOrderConf/o_save.do")
	public String save(CarOrderConf bean, Integer shopId,HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean,shopId);
		log.info("save CarOrderConf id={}", bean.getId());
		cmsLogMng.operating(request, "CarOrderConf.log.save",  "id=" + bean.getId());
		return "redirect:v_list.do";
	}
	@RequestMapping("/carOrderConf/o_update.do")
	public String update(CarOrderConf bean,Integer shopId ,Integer pageNo,HttpServletRequest request,ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateTime = simpleTime.format(new Date());
		Timestamp t = Timestamp.valueOf(dateTime);

		bean.setUpdateTime(t);
		bean=manager.update(bean,shopId);
		model.addAttribute("message", "global.success");
		log.info("update CarOrderConf success. id={}", bean.getId());
		cmsLogMng.operating(request, "CarOrderConf.log.update",  "id=" + bean.getId());
		return edit(bean.getId(), request, model);
	}
	@RequestMapping("/carOrderConf/o_updateCarOrderConfDetail.do")
	public String updateCarOrderConfDetail(Integer carOrderConfId,CarOrderConfDetail bean,Integer pageNo,HttpServletRequest request,ModelMap model) {
		WebErrors errors = validateUpdate(carOrderConfId, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarOrderConf carOrderConf = manager.findById(carOrderConfId);
		bean.setShop(carOrderConf.getShop());
		bean.setCarOrderConf(carOrderConf);
		if(bean.getId()==null){
			bean=carOrderConfDetailMng.save(bean);
		}else{
			bean=carOrderConfDetailMng.update(bean);
		}
		model.addAttribute("message", "global.success");
		log.info("update CarOrderConf success. id={}", bean.getId());
		cmsLogMng.operating(request, "CarOrderConf.log.update",  "id=" + bean.getCarOrderConf().getId());
		model.addAttribute("message", "global.success");
		return editCarOrderConfDetail(carOrderConfId, request, model);
	}
	@RequestMapping("/carOrderConf/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		CarOrderConf carOrderConf = new CarOrderConf();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		
		List<Dictionary2> listOrderType=DicMng.getList("order_type");
		model.addAttribute("carOrderConf", carOrderConf);
		model.addAttribute("listShop", listShop);
		model.addAttribute("listOrderType", listOrderType);
		return "car/add_carOrderConf";
	}
	
	@RequestMapping("/carOrderConf/o_delete.do")
	public String delete(Integer[] ids, Short orderType, String name ,Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		CarOrderConf[] beans = manager.deleteByIds(ids);
		for (CarOrderConf bean : beans) {
			log.info("delete CarOrderConf id={}", bean.getId());
			cmsLogMng.operating(request, "CarOrderConf.log.delete", "id="
					+ bean.getId() + ";name=" + bean.getName());
		}
		return list(orderType,name,pageNo, request, model);
	}
	private WebErrors validateSave(CarOrderConf bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		// TODO 验证是否为管理员，管理员不允许修改。
		return errors;
	}
	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		CarOrderConf entity = manager.findById(id);
		if (errors.ifNotExist(entity, CarOrderConf.class, id)) {
			return true;
		}
		return false;
	}
	@Autowired
	private CarOrderConfMng manager;
	@Autowired
	private CarOrderConfDetailMng carOrderConfDetailMng;
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private CmsLogMng cmsLogMng;
	@Autowired
	private DictionaryMng DicMng;
}
